package vincent.com.layouttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.lang.ref.WeakReference;

import vincent.com.layouttest.view.SwitchButton;

/**
 * Created by zhengwenchen on 2017/1/16.
 */

public class WifiActivity extends ColorStatusActivity implements View.OnClickListener{

    private SwitchButton switchBtn;
    Receiver mReceiver;

    WifiManager mWifiManager;

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_wifi, rootView, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        switchBtn = (SwitchButton)findViewById(R.id.switch_btn);
        switchBtn.setOnClickListener(this);

        mWifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);

        mReceiver = new Receiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.switch_btn:
                toggleWifi();
                break;
        }
    }

    public void toggleWifi(){
        if(mWifiManager.isWifiEnabled()){
            mWifiManager.setWifiEnabled(false);
        }else{
            mWifiManager.setWifiEnabled(true);
        }
    }

    class Receiver extends BroadcastReceiver {

        WeakReference<WifiActivity> ref;

        Receiver(WifiActivity activity) {
            ref = new WeakReference<WifiActivity>(activity);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            WifiActivity instance = ref.get();
            if (instance == null) {
                return;
            }

            String action = intent.getAction();
            if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)){
                int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);

                switch (wifiState){
                    case WifiManager.WIFI_STATE_ENABLED:
                        onWifiEnabled();
                        break;
                    case WifiManager.WIFI_STATE_DISABLED:
                        onWifiDisabled();
                        break;
                }
            }
        }
    }

    public void onWifiDisabled(){
        switchBtn.setOpen(false);
    }

    public void onWifiEnabled(){
        switchBtn.setOpen(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mReceiver != null){
            unregisterReceiver(mReceiver);
        }
    }
}
